package android.visittool.network;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.os.AsyncTask;

public class Api  extends AsyncTask<String, Void, Void> {

	public final String url = "http://test.smartservice.by/";

	public String answer;

	public boolean finish = false;
	
	protected Void doInBackground(String... params) {
	//public static String Query(String params) {
		while (true) {
			try {
				URL apiUrl = new URL (url + params[0]);				
		        URLConnection conn = apiUrl.openConnection();
		        InputStreamReader rd = new InputStreamReader(conn.getInputStream());
		        //BufferedInputStream rd = new BufferedInputStream(conn.getInputStream());
		        StringBuilder answer = new StringBuilder();	 
	            
		        int n = 0;
		        char[] buffer = new char[40000];
		        while (n >= 0)
		        {
		            n = rd.read(buffer, 0, buffer.length);
		            if (n > 0)	            
		            	answer.append(buffer, 0, n);                    	            
		        }
		        if (answer.toString() == "") {
		        	continue;
		        }			
		        this.answer = answer.toString();
		        finish = true;
		        break;
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
		return null;
	}
   
}
